

package com.hazelcast.client.impl.statistics;

import com.hazelcast.client.impl.connection.ClientConnectionManager;
import com.hazelcast.internal.metrics.DynamicMetricsProvider;
import com.hazelcast.internal.metrics.MetricDescriptor;
import com.hazelcast.internal.metrics.MetricsCollectionContext;
import com.hazelcast.internal.nio.Connection;

import static com.hazelcast.internal.metrics.MetricDescriptorConstants.CLUSTER_DISCRIMINATOR_ENDPOINT;
import static com.hazelcast.internal.metrics.MetricDescriptorConstants.CLUSTER_PREFIX_CONNECTION;

class ClusterConnectionMetricsProvider implements DynamicMetricsProvider {
    private final ClientConnectionManager clientConnectionManager;

    ClusterConnectionMetricsProvider(ClientConnectionManager clientConnectionManager) {
        this.clientConnectionManager = clientConnectionManager;
    }

    @Override
    public void provideDynamicMetrics(MetricDescriptor descriptor, MetricsCollectionContext context) {
        for (Connection connection : clientConnectionManager.getActiveConnections()) {
            context.collect(descriptor.withPrefix(CLUSTER_PREFIX_CONNECTION).withDiscriminator(CLUSTER_DISCRIMINATOR_ENDPOINT, connection.getRemoteAddress().toString()), connection);
        }
    }
}
